package it.xiuxian.homepage.fragment

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import it.xiuxian.homepage.R
import it.xiuxian.homepage.bean.GongXian
import it.xiuxian.homepage.bean.MonGongXianBean
import it.xiuxian.homepage.databinding.HomepageGongxianFragmentBinding
import it.xiuxian.homepage.presenter.GongXianFragmentPresenter
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseFragment
import it.xiuxian.lib.utils.ImageUtil
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector

@Route(path = ArouterAddress.GONGXIANFRAGMENT)
class GongXianFragment:BaseFragment<GongXianFragmentPresenter>() {
    var adapter1: SlimAdapter?=null
    var binding:HomepageGongxianFragmentBinding?=null
    var adapter2: SlimAdapter?=null
    override val layoutId: Int
        get() = R.layout.homepage_gongxian_fragment
    override val presenter: GongXianFragmentPresenter
        get() = GongXianFragmentPresenter()

    override fun isEmpty(textView: TextView?): Boolean {
        return false
    }
    var i =1
    var j =1
    override fun initView() {
      binding=  getViewDataBinding<HomepageGongxianFragmentBinding>()
         binding!!.rv.layoutManager = LinearLayoutManager(requireActivity())
        binding!!.rv1.layoutManager = LinearLayoutManager(requireActivity())
         adapter1 = SlimAdapter.create().register<GongXian>(R.layout.homepage_paihang_item,
              object:SlimInjector<GongXian> {
                  override fun onInject(
                      data: GongXian?,
                      injector: IViewInjector<out IViewInjector<*>>?
                  ) {
                      injector!!.text(R.id.tv_paiming,(i++).toString())
                          .text(R.id.tv_name,data!!.nickname)
                  }
              }).attachTo(binding!!.rv).attachTo(binding!!.rv1)
        adapter2 = SlimAdapter.create().register<GongXian>(R.layout.homepage_paihang_item,
            object:SlimInjector<GongXian> {
                override fun onInject(
                    data: GongXian?,
                    injector: IViewInjector<out IViewInjector<*>>?
                ) {
                    injector!!.text(R.id.tv_paiming,(j++).toString())
                        .text(R.id.tv_name,data!!.nickname)
                }
            }).attachTo(binding!!.rv1)

    }

    override fun initEvent() {
       // super.initEvent()
        binding!!.llYuepai.setOnClickListener { ARouter.getInstance().build(ArouterAddress.PAIHANGBANGACTIVITY).withString("flag","1").navigation() }
        binding!!.llZongpai.setOnClickListener { ARouter.getInstance().build(ArouterAddress.PAIHANGBANGACTIVITY).withString("flag","2").navigation() }
    }
    /*override fun initData() {

    }*/

    override fun loadData() {
        mPresenter!!.getMonGongxian()
        mPresenter!!.getZongGongxian()
    }
    fun setGongXian(data: MonGongXianBean) {
        ImageUtil.setImage(data.list[0].faceurl,binding!!.image1)
        adapter1!!.updateData(if(data.list.size>=3) data.list.subList(0,3) else data.list)
    }

    fun setZong(data: MonGongXianBean) {
        ImageUtil.setImage(data.list[0].faceurl,binding!!.image2)
        adapter2!!.updateData(if(data.list.size>=3) data.list.subList(0,3) else data.list)
    }
}